<template>
  <Header :title="title"></Header>
  <div style="background: #f3f3f3">
    <!-- 商品列表 -->
    <div class="fui-content">
      <div class="fui-content-inner">
        <div class="fui-goods-group block">
          <!-- 单个商品 -->
          <div
            class="fui-goods-item"
            @click="go_url(item.id)"
            v-for="item in list"
            :key="item.id"
          >
            <img class="image" :src="item.pic" />
            <div class="detail">
              <div class="name">
                {{ item.title }}
              </div>
              <div
                style="
                  line-height: 0.7rem;
                  height: 0.7rem;
                  color: #b2b2b2;
                  font-size: 0.6rem;
                  margin-top: 0.2rem;
                  text-decoration: line-through;
                "
              ></div>
              <div class="price">
                <span class="text">￥100</span>
                <span class="buy">销量：10</span>
              </div>
            </div>
          </div>
          <!-- 单个商品 -->
        </div>
        <div
          style="text-align: center; padding: 10px 0; background: #f9f9f9"
          v-if="noData"
        >
          没有更多数据了~
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Header from "../../components/Header.vue";
import { useRoute, useRouter } from "vue-router";
import { reactive, toRefs } from "vue";
import { GoodsLists } from "../../network/index.js";

export default {
  components: {
    Header,
  },
  setup() {
    const data = reactive({
      list: [],
      page: 1,
      noData: false,
    });
    const router = useRouter();
    const route = useRoute();
    const go_url = (id) => {
      router.push(`/goods_details?id=${id}`);
    };
    const getGoodsLists = () => {
      GoodsLists({ ...route.query, page: data.page }).then((res) => {
        if (!res || res.length == 0) {
          window.onscroll = null;
          data.noData = true;
        }
        data.list = [...data.list, ...res];
      });
    };
    getGoodsLists();
    // 滑动到底部事件
    window.onscroll = function () {
      var scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      var scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      var clientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      if (Math.ceil(scrollTop + clientHeight) >= scrollHeight) {
        data.page++;
        getGoodsLists();
      }
    };
    return {
      go_url,
      title: "列表",
      ...toRefs(data),
    };
  },
};
</script>
<style scoped>
/* 顶部筛选 */
.sort {
  position: relative;
  width: 100%;
  padding: 0.4rem 0;
  background: #fff;
  -webkit-box-align: center;
  border-bottom: 1px solid #e7e7e7;
}
.sort .item {
  position: relative;
  width: 1%;
  display: table-cell;
  text-align: center;
  font-size: 0.7rem;
  border-left: 1px solid #e7e7e7;
  color: #666;
}
.sort .item:first-child {
  border: 0;
}
.on .text {
  color: #fd5454;
}
/* 商品 */
.fui-content {
  position: absolute;
  right: 0;
  left: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.fui-content-inner {
  box-sizing: border-box;
  border-top: 1px solid transparent;
  margin-top: -1px;
}
.fui-goods-group {
  height: auto;
  overflow: hidden;
  background: #f9f9f9;
}
.fui-goods-group.block {
  padding: 0.2rem;
}
/* 单个商品 */
.fui-goods-group .fui-goods-item {
  width: 50%;
  float: left;
  border-bottom: 0;
  background: none;
  padding: 0.25rem;
  display: block;
}
.fui-goods-item {
  position: relative;
  height: auto;
  padding: 0.8rem;
  border-bottom: 1px solid #e7e7e7;
  background: #fff;
  overflow: hidden;
  display: flex;
}
.image {
  width: 100%;
  overflow: hidden;
  margin: 0;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  float: none;
}
/* 商品名 */
.detail {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  background: #fff;
  padding-left: 0.5rem;
  padding: 0.5rem;
}
.name {
  height: 1.7rem;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 0.65rem;
  line-height: 0.9rem;
  margin-top: 0;
  color: #262626;
}
.price {
  position: relative;
  display: flex;
  align-items: center;
  font-size: 0.7rem;
  margin-top: 0;
}
.price .text {
  flex: 1;
  color: #ff5555;
  font-size: 0.8rem;
}
.buy {
  text-align: center;
  color: #666;
  font-size: 0.6rem;
}
</style>